package com.springboot.tr.task;

/**
 * @description:
 * @author: ZZZ
 * @time: 2021/8/8 16:14
 */

import com.springboot.sba.D9_DynamicTask.task.ScheduledTaskJob;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class CollectPerfomanceDataTask extends ScheduledTaskJob {

    /**
     * 存放已经启动的任务map
     */
    private ConcurrentHashMap<String, LevelTIme> scheduledFutureMap = new ConcurrentHashMap<>();


    @Override
    public void run() {
        CollectZk();

    }

    public void CollectZk() {
        Random random = new Random();
        int i = random.nextInt(5);
        System.out.println("==========保存ZK性能数据  " + i+"  ============");
        if (3 == 3) {
            if (scheduledFutureMap.containsKey("zk")) {
                LevelTIme levelTIme = scheduledFutureMap.get("zk");
                //如果等级等于7 告警已经没有意义了
                if(levelTIme.getLevel()==7){
                    System.out.println("level = 7");
                    return;
                }
                LocalDateTime nowTime = LocalDateTime.now();
                if (nowTime.isAfter(levelTIme.getLocalDateTime())) {
                    int nextLevel = levelTIme.getLevel() + 1;
                    LevelTIme nextLevelTime = new LevelTIme(nextLevel, nowTime.plusMinutes(LevelEnum.getLevelMinute(nextLevel)));
                    System.out.println("发送邮件：zk状态异常"+" level="+nextLevel);
                    scheduledFutureMap.put("zk", nextLevelTime);
                }
            } else {
                //第一次告警
                LevelTIme levelTIme = new LevelTIme(1, LocalDateTime.now().plusMinutes(LevelEnum.getLevelMinute(1)));
                scheduledFutureMap.put("zk", levelTIme);
                System.out.println("发送邮件：zk状态异常"+" level=1");
            }
        } else {
            //告警恢复
            if (scheduledFutureMap.containsKey("zk")) {
                scheduledFutureMap.remove("zk");
                System.out.println("发送邮件：zk状态恢复");
            }
        }
    }
}
